var util=require('../../utils/util')
var api = require('../../config/api.js');
import { getCreateQrocde } from '../../config/api.js';
//var drawQrcode=require('../../utils/qcode/weapp.qrcode.esm.js');
import drawQrcode from '../../utils/qcode/weapp.qrcode.esm.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        openIdNew:'',
        imageBase64:''


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getInItCount();
        //初始用户
        this.initUser();

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    initUser(){
         //获取uidid
      var userinfo = wx.getStorageSync('userInfoDto');

      if (typeof (userinfo.userId) != "undefined") {
          this.initQCode(userinfo.userId)
          this.setData({
            openIdNew:userinfo.userId,
        })
        //this.getWebQcode(userinfo)

      }

    },

    //前端读取二维码
    getWebQcode(userinfo){
      var accessToken = wx.getStorageSync("accessToken");
      var that=this;
      wx.request({
        //注意：下面的access_token值可以不可以直接复制使用，需要自己请求获取
        url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='+accessToken,
        data: {
          scene: userinfo.userId,
          page: "pages/index/index"  //这里按照需求设置值和参数   
        },
        method: "POST",
        responseType: 'arraybuffer',  //设置响应类型
        arraybuffer: true,    //转码，注意别忘了这个属性，不然会乱码
        success(res) {
          console.log(res)
          var imgurl ='data:image/jpeg;base64,'+ wx.arrayBufferToBase64(res.data);  //对数据进行转换操作
          that.setData({
            imageBase64:imgurl
          })
        },
        fail(e) {
          console.log(e)
        }
      })

    },


    /**
   * 创建二维码
   * @param {} text  内容体
   */
  initQCode(text) {
        
    const query = wx.createSelectorQuery()
    query.select('#myQrcode')
      .fields({
        node: true,
        size: true
      })
      .exec((res) => {
        var canvas = res[0].node
        // 调用方法drawQrcode生成二维码
        drawQrcode({
          canvas: canvas,
          canvasId: 'myQrcode',
          width: 260,
          padding: 30,
          background: '#ffffff',
          foreground: '#000000',
          text
        })

        // 获取临时路径（得到之后，想干嘛就干嘛了）
        wx.canvasToTempFilePath({
          canvasId: 'myQrcode',
          canvas: canvas,
          x: 0,
          y: 0,
          width: 260,
          height: 260,
          destWidth: 260,
          destHeight: 260,
          success(res) {
            console.log('二维码临时路径：', res.tempFilePath)
          },
          fail(res) {
            console.error(res)
          }
        })
      })
    },

    reprotList(){
        wx.navigateTo({
            url: '/pages/report/reportlist'
          })
    },

    reprotList(){
        wx.navigateTo({
            url: '/pages/report/reportlist'
          })
    },

    taskList(){
      wx.navigateTo({
        url: '/pages/task/mytasklist'
      })
    },

    myRewardsList(){
        wx.navigateTo({
            url: '/pages/reward/rewardlist'
          })
    },
    userinfo(){
        wx.navigateTo({
            url: '/pages/home/userinfo'
          })

    },
    question(){
        wx.navigateTo({
            url: '/pages/question/question'
          })

    },


    todaycall(){
      wx.navigateTo({
        url: '/pages/reward/myrewardlist'
      })

    },

    tojf(){
      wx.navigateTo({
        url: '/pages/anwser/anwserlist'
      })
    },

    myScoreList(){
      wx.navigateTo({
        url: '/pages/produce/producescore'
      })
    },


    //获取收入
    getInItCount(){
        var _t = this;
    console.log(_t.data.taskId)
    util.request(api.myRewardsCount ,'', 'POST').then(function (res) {
      console.log(res)
      if (res.code == 200) {
        console.log(res.data)
        _t.setData({
            cashAll: res.data.cashAll,
            cashAllToday: res.data.cashAllToday,
            userScores: res.data.userScores

        })

      } else {
        util.showErrorToast(res.message);
      }

    }).catch((err) => {
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });

    }
})